<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="付款单编号" prop="payOrderNo">
        <el-input
          v-model="queryParams.payOrderNo"
          placeholder="请输入付款计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主姓名" prop="ownerName">
        <el-input
          v-model="queryParams.ownerName"
          placeholder="请输入业主姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item><br>
      <el-form-item label="付款状态">
        <el-select v-model="queryParams.payState" clearable>
          <el-option
            v-for="item in dict.type.sys_payment_type"
            :value="item.value"
            :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账单类型">
        <el-select v-model="queryParams.billType" clearable>
          <el-option
            v-for="item in dict.type.sys_payment_plan_type"
            :value="item.value"
            :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收房合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入收房合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item><br>
      <el-form-item label="付款日期">
        <el-date-picker
          v-model="queryParams.beginDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期" clearable>
        </el-date-picker>
        <el-date-picker
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期" clearable>
        </el-date-picker>
      </el-form-item><br>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['affairs:cwPaymentPlan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="收房付款单" name="first">
        <el-table v-loading="loading" :data="cwPaymentPlanList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="收房合同编号" align="center" prop="contractNo" />
          <el-table-column label="房产信息" align="center" prop="houseName" />
          <el-table-column label="业主姓名" align="center" prop="ownerName" />
          <el-table-column label="支付金额" align="center" prop="paymentAmount" />
          <el-table-column label="账期" align="center" prop="payNumber">
            <template slot-scope="scope">
              <span>第{{scope.row.payNumber}}期</span>
            </template>
          </el-table-column>
<!--          <el-table-column label="款项" align="center" prop="itemType" />-->
          <el-table-column label="付款状态" align="center" prop="payState">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.affairs_receipt_status" :value="scope.row.payState"/>
            </template>
          </el-table-column>
          <el-table-column label="账单类型" align="center" prop="billType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_payment_plan_type" :value="scope.row.billType"/>
            </template>
          </el-table-column>
          <el-table-column label="应付款日期" align="center" prop="payableDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payableDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="付款日期" align="center" prop="paidTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.paidTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="支付方式" align="center" prop="payMethod">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.affairs_pay_way" :value="scope.row.payMethod"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <span v-if="scope.row.payState==2">--</span>
              <span v-if="scope.row.payState==3">已终止</span>
              <span v-if="scope.row.payState==4">已冻结</span>
              <el-link type="primary" v-if="scope.row.payState==1" @click="toPayment(scope.row.id,scope.row.paymentAmount)">付款</el-link>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
      <el-tab-pane label="租务付款单" name="second">
        <el-table v-loading="loading" :data="cwPaymentPlanList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="收房合同编号" align="center" prop="contractNo" />
          <el-table-column label="房产信息" align="center" prop="houseName" />
          <el-table-column label="业主姓名" align="center" prop="ownerName" />
          <el-table-column label="支付金额" align="center" prop="paymentAmount" />
          <el-table-column label="账期" align="center" prop="payNumber">
            <template slot-scope="scope">
              <span>第{{scope.row.payNumber}}期</span>
            </template>
          </el-table-column>
          <el-table-column label="款项" align="center" prop="itemType" />
          <el-table-column label="付款状态" align="center" prop="payState">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.affairs_receipt_status" :value="scope.row.payState"/>
            </template>
          </el-table-column>
          <el-table-column label="账单类型" align="center" prop="billType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_payment_plan_type" :value="scope.row.billType"/>
            </template>
          </el-table-column>
          <el-table-column label="应付款日期" align="center" prop="payableDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payableDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="付款日期" align="center" prop="paidTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.paidTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="支付方式" align="center" prop="payMethod">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.affairs_pay_way" :value="scope.row.payMethod"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <span v-if="scope.row.payState==2">--</span>
              <span v-if="scope.row.payState==3">已终止</span>
              <span v-if="scope.row.payState==4">已冻结</span>
              <el-link type="primary" v-if="scope.row.payState==1" @click="toPayment(scope.row.id,scope.row.paymentAmount)">付款</el-link>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
      <el-tab-pane label="金融服务付款单" name="third">
        <el-table v-loading="loading" :data="cwPaymentPlanList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="收房合同编号" align="center" prop="contractNo" />
          <el-table-column label="房产信息" align="center" prop="houseName" />
          <el-table-column label="业主姓名" align="center" prop="ownerName" />
          <el-table-column label="支付金额" align="center" prop="paymentAmount" />
          <el-table-column label="账期" align="center" prop="payNumber">
            <template slot-scope="scope">
              <span>第{{scope.row.payNumber}}期</span>
            </template>
          </el-table-column>
          <!--<el-table-column label="款项" align="center" prop="itemType" />-->
          <el-table-column label="付款状态" align="center" prop="payState">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.affairs_receipt_status" :value="scope.row.payState"/>
            </template>
          </el-table-column>
          <el-table-column label="账单类型" align="center" prop="billType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_payment_plan_type" :value="scope.row.billType"/>
            </template>
          </el-table-column>
          <el-table-column label="应付款日期" align="center" prop="payableDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payableDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="付款日期" align="center" prop="paidTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.paidTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="支付方式" align="center" prop="payMethod">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.affairs_pay_way" :value="scope.row.payMethod"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <span v-if="scope.row.payState==2">--</span>
              <span v-if="scope.row.payState==3">已终止</span>
              <span v-if="scope.row.payState==4">已冻结</span>
              <el-link type="primary" v-if="scope.row.payState==1" @click="toPayment(scope.row.id,scope.row.paymentAmount)">付款</el-link>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
    </el-tabs>

    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form2">
        <span>付款金额：</span><span>{{this.form2.realPaymentAmount}}</span><br>
        <el-form-item label="支付方式">
          <el-select v-model="form2.payMethod" clearable>
            <el-option
              v-for="item in dict.type.affairs_pay_way"
              :value="item.value"
              :label="item.label">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmThePayment">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCwPaymentPlan, getCwPaymentPlan, delCwPaymentPlan, addCwPaymentPlan, updateCwPaymentPlan,payForTheOrder } from "@/api/affairs/cwPaymentPlan";

export default {
  name: "CwPaymentPlan",
  dicts: ['affairs_receipt_status','sys_payment_plan_type','sys_payment_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 付款计划管理表格数据
      cwPaymentPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemflag: 1,
        payOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        payType: null,
        payState: null,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withPayPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        payableDate: null,
        paidTime: null,
        payNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        totalPayNumber: null,
        repayType: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      //默认标签页
      activeName: 'first',
      dialogFormVisible: false,
      form2: {
        // aaa: 'a526',
        //需要支付的id
        paymentId: null,
        //付款的金额
        realPaymentAmount: null,
        //需要支付的方式
        payMethod: null,
      },
      formLabelWidth: '120px'
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //付款按钮
    toPayment(id,paymentAmount){
      //初始化支付数据
      this.form2.paymentId=undefined;
      this.form2.realPaymentAmount=undefined;
      //赋值数据
      this.form2.paymentId=id;
      this.form2.realPaymentAmount=paymentAmount;
      this.dialogFormVisible=true;
    },
    //确认付款
    confirmThePayment(){
      payForTheOrder(this.form2).then(res=>{
        this.$modal.msgSuccess("处理成功");
        this.dialogFormVisible = false;
        this.getList();
      });
    },
    handleClick(tab, event) {
      //每次查询前都前往第一页
      this.queryParams.pageNum=1;
      //判断是哪个页面
      if (tab.name=="first"){
        //查询收房付款单
        this.queryParams.itemflag=1;
      }else if (tab.name=="second"){
        //查询租务付款单
        this.queryParams.itemflag=2;
      }else {
        //查询金融服务付款单
        this.queryParams.itemflag=3;
      }
      this.getList();
      console.log(tab, event);
    },
    /** 查询付款计划管理列表 */
    getList() {
      this.loading = true;
      // updateCwPaymentPlan(this.queryParams).then(res=>{
      //
      // })
      listCwPaymentPlan(this.queryParams).then(response => {
        this.cwPaymentPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        payOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        payType: null,
        payState: null,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withPayPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        payableDate: null,
        paidTime: null,
        payNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        totalPayNumber: null,
        repayType: null,
        remark: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('affairs/cwPaymentPlan/export', {
        ...this.queryParams
      }, `cwPaymentPlan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
